package com.gdhz.xiaohengqin.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gdhz.xiaohengqin.handler.AccessGateWebSocketHandler;
import com.gdhz.xiaohengqin.handler.MessageHandler;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.socket.client.WebSocketClient;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.List;

@Service
public class WebSocketClientService {
    private final WebSocketClient webSocketClient;
    private final ObjectMapper objectMapper;
    private final List<MessageHandler> messageHandlers;

    public WebSocketClientService(WebSocketClient webSocketClient, ObjectMapper objectMapper, List<MessageHandler> messageHandlers) {
        this.webSocketClient = webSocketClient;
        this.objectMapper = objectMapper;
        this.messageHandlers = messageHandlers;
    }

    public Mono<Void> connect(String url) {
        return webSocketClient.execute(
                URI.create(url),
                new AccessGateWebSocketHandler(objectMapper, messageHandlers)
        );
    }
}